package com.itfreer.bpm.workflow.service.disposal;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.itfreer.bpm.flow.api.IBpmProjectEntity;
import com.itfreer.bpm.flow.api.IUserNextTaskUser;
import com.itfreer.bpm.history.entity.BpmHistoryEntity;
import com.itfreer.bpm.messager.BpmMessage;
import com.itfreer.bpm.option.para.NextTaskInfo;
import com.itfreer.bpm.option.para.OptionInfo;
import com.itfreer.bpm.option.para.OptionType;
import com.itfreer.bpm.option.para.TaskInfo;
import com.itfreer.bpm.power.para.UserInfo;
import com.itfreer.bpm.workflow.drive.IWorkFlowDrive;
import com.itfreer.bpm.workflow.option.SignCommonTask;
import com.itfreer.bpm.workflow.option.SignMultiTask;
import com.itfreer.bpm.workflow.service.BpmWorkFlowCommonSignImp;
import com.itfreer.bpm.workflow.service.skip.BpmWorkFlowSkipImp;
import com.itfreer.bpm.workflow.userrole.IUserRole;
import com.itfreer.bpm.workflow.utils.BpmApprovalHistoryUtil;
import com.itfreer.bpm.workflow.utils.BpmHistoryEditUtil;
import com.itfreer.bpm.workflow.utils.BpmHistoryQueryUtil;
import com.itfreer.bpm.workflow.utils.BpmNextTaskUtils;
import com.itfreer.bpm.workflow.utils.BpmUserAuthUtils;

@Component
public class BpmWorkFlowDisposeImp {
	
	/**
	 * 流程驱动服务
	 */
	@Autowired
	protected IWorkFlowDrive workFlowDrive;
	
	/**
	 * 构建历史帮转工具
	 */
	@Autowired
	BpmHistoryEditUtil historyEditUtl;
	
	/**
	 * 构建用户审批历史
	 */
	@Autowired
	BpmApprovalHistoryUtil approvalUtil;
	
	/**
	 * 用户授权工具
	 */
	@Autowired
	BpmUserAuthUtils userAuthUtils;
	
	/**
	 * 审批过程查询接口
	 */
	@Autowired
	BpmHistoryQueryUtil historyQueryUtil;
	
	/**
	 * 用户角色接口
	 */
	@Autowired
	IUserRole userRole;
	/**
	 * 用户自定义操作下一步审批人接口
	 */
	@Autowired(required=false)
	IUserNextTaskUser userNextTaskUser;
	
	/**
	 * 下一个环节工具
	 */
	@Autowired
	BpmNextTaskUtils nextTaskUtils;
	/**
	 * 单节点会签提交
	 */
	@Autowired
	SignMultiTask signmultiTask;
	
	/**
	 * 通用流程驱动操作
	 */
	@Autowired
	BpmWorkFlowCommonSignImp commonSign;
	
	/**
	 * 多节点选择节点
	 */
	@Autowired
	SignCommonTask signCommonTask;
	
	@Autowired
	BpmWorkFlowSkipImp skipTask;
	
	private final static String optionName="退件";
	
	/**
	 * 用户自定义项目结束操作
	 * @param oInfo 操作类型
	 * @param entity 项目实体
	 * @param user 用户
	 * @return
	 */
	public BpmMessage userDispose(OptionInfo oInfo, IBpmProjectEntity entity, UserInfo user) {
		BpmMessage message=new BpmMessage();
		HashMap<String,Object> work_where=new HashMap<String,Object>();
		work_where.put("sexeid:=", oInfo.getSexeid());
		//检查校验是否是超级管理员
		if(!user.isSuperAdmin()) {
			work_where.put("toptassigneese:like",user.getUserId());
		}
		List<BpmHistoryEntity>toWorkList= historyQueryUtil.getWorkEntitys(null, work_where, null, 1000, 1);
		//检查项目是否拥有权限
		if(toWorkList==null || toWorkList.size()<1) {
			message.setStatus(false);
			message.setMessage("没有项目审核的权限");
			return message;
		}
		
		//当前环节的id
		String cTask=toWorkList.get(0).gettaskdefid();
		
		HashMap<String, Object> skipWhere=new HashMap<String, Object> ();
		skipWhere.put("sexeid:=", oInfo.getSexeid());
		HashMap<String, Integer> skipOrder=new HashMap<String, Integer> ();
		skipOrder.put("opttime", 0);
		
		List<BpmHistoryEntity> allHistory=historyQueryUtil.getEntitys(null, skipWhere, skipOrder, 2, 1);
		if(allHistory==null || allHistory.size()<2) {
			message.setStatus(false);
			message.setMessage("项目尚未启动，不能进行"+oInfo.getUserOption()+"操作");
			return message;
		}
		
		List<BpmHistoryEntity> lst=oInfo.getEditHistory();
		if(lst==null || lst.size()<1) {
			lst=toWorkList;
		}
		BpmHistoryEntity history=lst.get(0);
		String o_sassignees=history.getsassignees();
		history.sethistorystatus("0");//最新的历史记录
		history.setoptresult(oInfo.getUserOption());
		history.setoptassigneesid(user.getUserId());
		history.setoptassigneesname(user.getUserName());
		history.setoptorgid(user.getUserOrgCode());
		history.setoptorgname(user.getUserOrgName());
		history.setopttime(new Date());
		String sassignees=","+user.getUserId()+","+user.getUserName();
		
		if(o_sassignees==null) {
			
		}else if(!o_sassignees.contains(sassignees)){
			sassignees=sassignees+","+o_sassignees;
		}else {
			sassignees=o_sassignees;
		}
		history.setsassignees(sassignees);
		historyEditUtl.updateList(lst);
		
		//构建多余的退件记录
		BpmHistoryEntity ehistory=this.historyEditUtl.CreateHistory(oInfo.getSexeid(), oInfo.getbpmkey(), user, null, entity);
		ehistory.settaskdefid("U9");
		ehistory.settaskdefname(oInfo.getUserOption());
		ehistory.setoptresult(oInfo.getUserOption());
		ehistory.sethistorystatus("20");
		ehistory.setTaskstatus(oInfo.getUserOption());
		ehistory.setsassignees(sassignees);
		ehistory.setfromtaskdefid(history.gettaskdefid());
		ehistory.setfromtaskdefname(history.gettaskdefname());
		ehistory.setoptassigneesid(user.getUserId());
		ehistory.setoptassigneesname(user.getUserName());
		ehistory.setoptorgid(user.getUserOrgCode());
		ehistory.setoptorgname(user.getUserOrgName());
		ehistory.setopttime(new Date());
		this.historyEditUtl.addandSaveEntity(ehistory);
		//流程真实退件
		this.workFlowDrive.dispose(oInfo.getSexeid());
		
		return message;
	}
	
	/**
	 * 项目退件
	 * 1、当前环节如果不是流程的第一个环节，直接跳转到流程的第一个环节
	 * 2、当前环节如果是流程的第一个环节，直接流程退件
	 */
	public BpmMessage dispose(OptionInfo oInfo, IBpmProjectEntity entity, UserInfo user) {
		
		
		//return this.skip(oInfo, entity, user, "跳转");
		BpmMessage message=new BpmMessage();
		HashMap<String,Object> work_where=new HashMap<String,Object>();
		work_where.put("sexeid:=", oInfo.getSexeid());
		//检查校验是否是超级管理员
		if(!user.isSuperAdmin()) {
			work_where.put("toptassigneese:like",user.getUserId());
		}
		List<BpmHistoryEntity>toWorkList= historyQueryUtil.getWorkEntitys(null, work_where, null, 1000, 1);
		//检查项目是否拥有权限
		if(toWorkList==null || toWorkList.size()<1) {
			message.setStatus(false);
			message.setMessage("没有项目审核的权限");
			return message;
		}
		
		//当前环节的id
		String cTask=toWorkList.get(0).gettaskdefid();
		
		HashMap<String, Object> skipWhere=new HashMap<String, Object> ();
		skipWhere.put("sexeid:=", oInfo.getSexeid());
		HashMap<String, Integer> skipOrder=new HashMap<String, Integer> ();
		skipOrder.put("opttime", 0);
		
		List<BpmHistoryEntity> allHistory=historyQueryUtil.getEntitys(null, skipWhere, skipOrder, 2, 1);
		if(allHistory==null || allHistory.size()<2) {
			message.setStatus(false);
			message.setMessage("项目尚未启动，不需要退件");
			return message;
		}
		String firstTaskid=allHistory.get(1).gettaskdefid();
		if(firstTaskid!=null && !firstTaskid.equals(cTask)) {
			
			NextTaskInfo nextTaskInfo=new NextTaskInfo();
			
			BpmHistoryEntity ih=allHistory.get(1);
			TaskInfo itask=new TaskInfo();
			itask.setTaskName(ih.gettaskdefname());
			itask.setTaskdefinedid(ih.gettaskdefid());
			UserInfo iuser=new UserInfo();
			iuser.setUserId(ih.getoptassigneesid());
			iuser.setUserName(ih.getoptassigneesname());
			itask.getUser().add(iuser);
			itask.setTaskSelect(true);
			itask.setAllUserSelect();
			nextTaskInfo.getNextTask().add(itask);
			
			if(nextTaskInfo.getNextTask()!=null && nextTaskInfo.getNextTask().size()>0) {
				nextTaskInfo.getNextTask().get(0).setTaskSelect(true);
				nextTaskInfo.getNextTask().get(0).setAllUserSelect();
				if(nextTaskInfo.getNextTask().size()>1) {
					oInfo.setOptiontype(OptionType.ExclusiveSkip);
				}else {
					oInfo.setOptiontype(OptionType.CommonSkip);
				}
				oInfo.setNextTask(nextTaskInfo.getNextTask());
				
				message= skipTask.skip(oInfo, entity, user,optionName,optionName);
				
				/*HashMap<String, Object> overWhere=new HashMap<String, Object>();
				List<BpmHistoryEntity> lst	=historyQueryUtil.getOverWorkEntitys(null, overWhere, null, 1, 1);
				if(lst!=null && lst.size()>0) {
					lst.get(0).setTaskstatus("退件");
					this.historyEditUtl.updateEntity(lst.get(0));
				}*/
				return message;
			}
		}else {
			List<BpmHistoryEntity> lst=oInfo.getEditHistory();
			if(lst==null || lst.size()<1) {
				lst=toWorkList;
			}
			BpmHistoryEntity history=lst.get(0);
			String o_sassignees=history.getsassignees();
			history.sethistorystatus("0");//最新的历史记录
			history.setoptresult(optionName);
			history.setoptassigneesid(user.getUserId());
			history.setoptassigneesname(user.getUserName());
			history.setoptorgid(user.getUserOrgCode());
			history.setoptorgname(user.getUserOrgName());
			history.setopttime(new Date());
			String sassignees=","+user.getUserId()+","+user.getUserName();
			if(o_sassignees==null) {
				
			}else if(!o_sassignees.contains(sassignees)){
				sassignees=sassignees+","+o_sassignees;
			}else {
				sassignees=o_sassignees;
			}
			history.setsassignees(sassignees);
			historyEditUtl.updateList(lst);
			
			//构建多余的退件记录
			BpmHistoryEntity ehistory=this.historyEditUtl.CreateHistory(oInfo.getSexeid(), oInfo.getbpmkey(), user, null, entity);
			ehistory.settaskdefid("T9");
			ehistory.settaskdefname("已"+optionName);
			ehistory.setoptresult(optionName);
			ehistory.sethistorystatus("20");
			ehistory.setTaskstatus(optionName);
			ehistory.setsassignees(sassignees);
			ehistory.setfromtaskdefid(history.gettaskdefid());
			ehistory.setfromtaskdefname(history.gettaskdefname());
			ehistory.setoptassigneesid(user.getUserId());
			ehistory.setoptassigneesname(user.getUserName());
			ehistory.setoptorgid(user.getUserOrgCode());
			ehistory.setoptorgname(user.getUserOrgName());
			ehistory.setopttime(new Date());
			this.historyEditUtl.addandSaveEntity(ehistory);
			//流程真实退件
			this.workFlowDrive.dispose(oInfo.getSexeid());
		}
		return message;
	}
	
	/**
	 * 系统激活
	 * @param oInfo
	 * @param entity
	 * @param user
	 * @return
	 */
	public BpmMessage easter(OptionInfo oInfo, IBpmProjectEntity entity, UserInfo user) {
		//return this.skip(oInfo, entity, user, "跳转");
		BpmMessage message=new BpmMessage();
		HashMap<String,Object> work_where=new HashMap<String,Object>();
		work_where.put("sexeid:=", oInfo.getSexeid());
		//检查校验是否是超级管理员
		if(!user.isSuperAdmin()) {
			work_where.put("toptassigneese:like",user.getUserId());
		}
		List<BpmHistoryEntity>toWorkList= historyQueryUtil.getWorkEntitys(null, work_where, null, 1000, 1);
		//检查项目是否拥有权限
		if(toWorkList==null || toWorkList.size()<1) {
			message.setStatus(false);
			message.setMessage("没有项目审核的权限");
			return message;
		}
		
		//当前环节的id
		
		HashMap<String, Object> skipWhere=new HashMap<String, Object> ();
		skipWhere.put("sexeid:=", oInfo.getSexeid());
		skipWhere.put("optresult:=",optionName);
		HashMap<String, Integer> skipOrder=new HashMap<String, Integer> ();
		skipOrder.put("opttime", 1);
		
		List<BpmHistoryEntity> allHistory=historyQueryUtil.getEntitys(null, skipWhere, skipOrder, 1, 1);
		if(allHistory==null || allHistory.size()<1) {
			message.setStatus(false);
			message.setMessage("项目尚未启动，不需要退件");
			return message;
		}
		
		NextTaskInfo nextTaskInfo=new NextTaskInfo();
		
		BpmHistoryEntity ih=allHistory.get(0);
		TaskInfo itask=new TaskInfo();
		itask.setTaskName(ih.gettaskdefname());
		itask.setTaskdefinedid(ih.gettaskdefid());
		UserInfo iuser=new UserInfo();
		iuser.setUserId(ih.getoptassigneesid());
		iuser.setUserName(ih.getoptassigneesname());
		itask.getUser().add(iuser);
		itask.setTaskSelect(true);
		itask.setAllUserSelect();
		nextTaskInfo.getNextTask().add(itask);
		
		if(nextTaskInfo.getNextTask()!=null && nextTaskInfo.getNextTask().size()>0) {
			nextTaskInfo.getNextTask().get(0).setTaskSelect(true);
			nextTaskInfo.getNextTask().get(0).setAllUserSelect();
			if(nextTaskInfo.getNextTask().size()>1) {
				oInfo.setOptiontype(OptionType.ExclusiveSkip);
			}else {
				oInfo.setOptiontype(OptionType.CommonSkip);
			}
			oInfo.setNextTask(nextTaskInfo.getNextTask());
			
			return skipTask.skip(oInfo, entity, user,"激活",null);
		}
		return null;
	}
	
	
}
